package com.chm.fw.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.chm.fw.R;
import com.chm.fw.base.BaseActivity;
import com.chm.fw.utils.CameraUtils;

import java.io.File;

/**
 * <!--动态权限-->
 * <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 * 1、res 中新建文件夹“xml”以及相应文件
 * 2、在AndroidManifest.xml 中配置“android.support.v4.content.FileProvider”Android7.0访问手机内存文件
 */
public class CameraActivity extends BaseActivity implements View.OnClickListener {

    //authority = this.getPackageName() + ".fileprovider"
    private String authority;

    //存储照片文件夹路径
    private File fileDirName = new File(Environment.getExternalStorageDirectory().getPath(), "00CHM/");

    //头像
    private String fileAvatarName = "avatar" + ".png";
    private ImageView ivAvatar;

    //图片
    private String fileName = "pic" + ".png";
    private ImageView ivPic;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        initAuthority();
        initView();
    }

    private void initView() {
        ivAvatar = findViewById(R.id.a_camera_avatar_iv);
        findViewById(R.id.a_camera_avatar_btn1).setOnClickListener(this);
        findViewById(R.id.a_camera_avatar_btn2).setOnClickListener(this);

        ivPic = findViewById(R.id.a_camera_pic_iv);
        findViewById(R.id.a_camera_pic_btn1).setOnClickListener(this);
        findViewById(R.id.a_camera_pic_btn2).setOnClickListener(this);
    }

    private void initAuthority() {
        authority = this.getPackageName() + ".fileprovider";
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.a_camera_avatar_btn1://头像   拍摄照片
                CameraUtils.toTakePhoto(101, this, fileDirName, fileAvatarName, authority);
                break;
            case R.id.a_camera_avatar_btn2://头像   选择照片
                CameraUtils.toTakePicture(102, this);
                break;
            case R.id.a_camera_pic_btn1:
                CameraUtils.toTakePhoto(201, this, fileDirName, fileName, authority);
                break;
            case R.id.a_camera_pic_btn2:
                CameraUtils.toTakePicture(202, this);
                break;
        }
    }

    @Override
    public void onActivityResult(final int requestCode, int resultCode, Intent data) {
        Uri uriTempFile100 = Uri.parse("file://" + "/" + fileDirName.getPath() + "/" + fileAvatarName);
        Uri uriTempFile200 = Uri.parse("file://" + "/" + fileDirName.getPath() + "/" + fileName);
        if (resultCode == -1) {
            File file;
            Uri imageUri;
            switch (requestCode) {
                case 101:
                    file = new File(fileDirName, fileAvatarName);
                    imageUri = FileProvider.getUriForFile(this, authority, file);//通过FileProvider创建一个content类型的Uri
                    CameraUtils.startPhotoZoom(100, imageUri, uriTempFile100, this);
                    break;
                case 102:
                    imageUri = data.getData();
                    CameraUtils.startPhotoZoom(100, imageUri, uriTempFile100, this);
                    break;
                case 201:
                    file = new File(fileDirName, fileName);
                    imageUri = FileProvider.getUriForFile(this, authority, file);//通过FileProvider创建一个content类型的Uri
                    CameraUtils.startPhotoZoom(200, imageUri, uriTempFile200, this);
                    break;
                case 202:
                    imageUri = data.getData();
                    CameraUtils.startPhotoZoom(200, imageUri, uriTempFile200, this);
                    break;
            }
        }
        //裁剪之后的图片
        Bitmap photo;
        switch (requestCode) {
            case 100:
                if (data != null && data.getExtras() != null) {
                    photo = data.getExtras().getParcelable("data");//防止无效文件报错闪退
                    CameraUtils.saveBitmap(photo, fileDirName + "/" + fileAvatarName);//压缩图片
                }
                //photo = BitmapFactory.decodeStream(getContentResolver().openInputStream(uriTempFile100));
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        uploadPic(requestCode);
                    }
                }, 500);
                break;
            case 200:
                if (data != null && data.getExtras() != null) {
                    photo = data.getExtras().getParcelable("data");//防止无效文件报错闪退
                    CameraUtils.saveBitmap(photo, fileDirName + "/" + fileName);//压缩图片
                }
                //photo = BitmapFactory.decodeStream(getContentResolver().openInputStream(uriTempFile200));
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        uploadPic(requestCode);
                    }
                }, 500);
                break;
        }
    }

    private void uploadPic(int requestCode) {
        File file;
        switch (requestCode) {
            case 100:
                file = new File(fileDirName, fileAvatarName);
                Glide.with(this)
                        .load(file)
                        .apply(RequestOptions.bitmapTransform(new CircleCrop()))//圆型图片
                        .into(ivAvatar);
                break;
            case 200:
                file = new File(fileDirName, fileName);
                Glide.with(this)
                        .load(file)
                        .into(ivPic);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        File[] files = fileDirName.listFiles();
        if (files != null) {
            for (File file : files) {
                String name = file.getName();
                if (name.startsWith("avatar") || name.startsWith("pic")) {
                    if (file.delete()) {
                        //toastGo("本地照片成功删除",0);
                    }
                }
            }
        }
        super.onDestroy();
    }
}
